package com.hjm.project.merchantInfo.merchantInfo.util;

import com.alibaba.fastjson.JSONObject;
import com.hjm.project.agentInfo.agentActActive.utils.AgentPriceCommon;
import com.hjm.project.agentInfo.agentPolicy.domain.AgentPolicy;
import com.hjm.project.merchantInfo.merchantRate.domain.MerchantRate;

import java.math.BigDecimal;
import java.util.List;

public class MerchantRateUtils {

    public static BigDecimal appendStr(BigDecimal str){
        String plainString = str.toPlainString();
        if (plainString.length() == 3) {
            plainString = plainString + "0";
            return new BigDecimal(plainString);
        }
        return str;
    }

    /**
     * 转换商户费率到页面
     * @param merchantRates
     * @return
     */
    public static MerchantCommon dbToView(List<MerchantRate> merchantRates){
        MerchantCommon policyInfoVo = new MerchantCommon();
        for (MerchantRate rate : merchantRates) {
            BigDecimal price = new BigDecimal(rate.getRateCode()).stripTrailingZeros();
            price = appendStr(price);
            Integer fj = Integer.parseInt(rate.getRateAffix());
            if (rate.getRateType() == 0 && rate.getCardType() == 2){ //刷卡贷记卡
                policyInfoVo.setS0_shuakaDaijika(price);
                policyInfoVo.setS0_shuakaDaijikaFj(Integer.parseInt(rate.getRateAffix()));
            }
            if (rate.getRateType() == 0 && rate.getCardType() == 1  && rate.getIsFd() == 0){ //刷卡借记卡
                policyInfoVo.setS0_shuakaJiejika(price);
                policyInfoVo.setS0_shuakaJiejikaFj(fj);
            }
            if (rate.getRateType() == 0 && rate.getCardType() == 1 && rate.getIsFd() == 1){ //刷卡借记卡
                policyInfoVo.setS0_shuakaJiejikaFd(Integer.parseInt(rate.getRateCode()));
            }

            if (rate.getRateType() == 4 && rate.getCardType() == 2){ //银联二维码贷记卡
                policyInfoVo.setS0_qrDaijika(price);
                policyInfoVo.setS0_qrDaijikaFj(fj);
            }

            if (rate.getRateType() == 4 && rate.getCardType() == 1){ //银联二维码借记卡
                policyInfoVo.setS0_qrJiejika(price);
                policyInfoVo.setS0_qrJiejikaFd(30);
                policyInfoVo.setS0_qrJiejikaFj(fj);
            }

            if (rate.getRateType() == 1){ //云闪付
                policyInfoVo.setYunshanfu0(price);
                policyInfoVo.setYunshanfu0Fj(fj);
            }
            if (rate.getRateType() == 2){ //微信
                policyInfoVo.setSaoma0Wx(price);
                policyInfoVo.setSaoma0FjWx(fj);
            }

            if (rate.getRateType() == 3){ //支付宝
                policyInfoVo.setSaoma0Zfb(price);
                policyInfoVo.setSaoma0FjZfb(fj);
            }
        }
        return policyInfoVo;
    }

    public static JSONObject getReqBody(MerchantCommon mc){
        JSONObject data = new JSONObject();
        data.put("dcRate",mc.getS0_shuakaJiejika());      //借记卡比例：0.60%
        data.put("dcMax",mc.getS0_shuakaJiejikaFd());       //借记卡封顶：35
        data.put("ccRate",mc.getS0_shuakaDaijika());        //贷记卡比例：0.60%
        data.put("feeRateDNfc",mc.getYunshanfu0());        //云闪付借记卡费率：0.40%
        data.put("feeRateCNfc",mc.getYunshanfu0());         //云闪付贷记卡费率：0.40%
        data.put("wechatFee",mc.getSaoma0Wx());             //微信费率：0.40%
        data.put("alipayFee",mc.getSaoma0Zfb());            //支付宝费率：0.40%
        data.put("unionPayDc",mc.getS0_qrJiejika());        //银联二维码借记卡费率：0.40%
        data.put("unionPayCc",mc.getS0_qrDaijika());        //银联二维码贷记卡费率：0.40%
        data.put("cashFeeAmt",mc.getS0_qrDaijikaFj());      //附加费：3.00
        return data;
    }
}
